<?php

namespace plugin\kb_blog\app\admin\controller;

use support\Request;
use support\Response;
use plugin\kb_blog\app\model\Note;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use plugin\kb_blog\app\model\Option;

/**
 * 博客笔记 
 */
class NoteController extends Crud
{

    /**
     * @var Note
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Note;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('note/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        $value = Option::where("name", "api")->value("value");
        $data = [];
        if ($value) {
            $data["api"] = $value;
        }
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('note/insert', $data);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        $value = Option::where("name", "api")->value("value");
        $data = [];
        if ($value) {
            $data["api"] = $value;
        }
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('note/update', $data);
    }
}
